<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\Controller;
use App\Models\customer\NodeModel;
use App\Models\customer\WarnModel;
use App\Models\FlightItemModel;
use Illuminate\Http\Request;

class Warn extends Controller
{

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $customer_id = $request->input('customer_id', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = WarnModel::query()->with(['asn', 'customer', 'warn_node', 'repair_node']);

        if($customer_id){
            $query = $query->where('customer_id', $customer_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $flight_id = $item->asn->take_order->flight_id ?? 0;
            $route_find = FlightItemModel::where('flight_id', $flight_id)->where('destination_id', $item->asn->des_country_id)->first();
            if($route_find){
                $route_name = $route_find->route->name ?? '';
            }else{
                $route_name = '';
            }
            $res = [
                'id' => $item->id,
                'take_order_sn' => $item->asn->take_order->sn ?? '',
                'bigBagID' => $item->asn->bigBagID ?? '',
                'waybillUrl' => $item->asn->waybillUrl ?? '',
                'logisticsOrderCode' => $item->asn->logisticsOrderCode ?? '',
                'customer_name' => $item->customer->name ?? '',
                'route_name' => $route_name,
                'warn_node_name' => $item->warn_node->name_cn ?? '',
                'over_time' => $item->over_time,
                'status_text' => WarnModel::STATUS[$item->status] ?? '',
                'repair_node_name' => $item->repair_node->name_cn ?? '',
                'warn_time' => $item->warn_time
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:补录轨迹
     * User: xiangzi
     * Date: 2024/11/8
     * Time: 下午2:20
     * @return \Illuminate\Http\JsonResponse
     */
    public function repair(Request $request){
        $id = $request->input('id', []);
        $node_id = $request->input('node_id');

        $find = WarnModel::find($id);
        if(!$id){
            return $this->error('请选择补录节点');
        }

        if(!$node_id){
            return $this->error('请选择节点');
        }

        WarnModel::whereIn('id', $id)->update(['repair_node_id' => $node_id, 'status' => 2]);

        return $this->success();
    }


}
